package com.topfinance.cnaps2.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;

import com.topfinance.cnaps2.dto.SysOrgDto;
import com.topfinance.cnaps2.ebo.SysOrgEbo;
import com.topfinance.cnaps2.service.ISysOrgManager;
import com.topfinance.frame.dao.HibernateEntityDao;
import com.topfinance.frame.exception.AppException;

/**
 * 该类自动实现了dao的一般方法，如add, remove, update
 * 在service中出现的大部分情况下是在向后台提交数据前的处理(如校验)及获取后台数据后的处理等
 * @author
 *
 */

public class SysOrgManagerImpl extends HibernateEntityDao<SysOrgEbo> implements ISysOrgManager {

	private static final Log log = LogFactory.getLog(SysOrgManagerImpl.class);

	public void addOrg(SysOrgEbo o) throws AppException{
    	save(o);
    }
	
	public void addOrgDto(SysOrgDto ot) throws AppException{
		//允许父ID为空
		SysOrgEbo po = null;
		if(ot.getPID() != null && ot.getPID()>0)
			po = getOrg(ot.getPID());
		SysOrgEbo o = new SysOrgEbo();
		BeanUtils.copyProperties(ot, o);
		o.setPORG(po);
		addOrg(o);
    }
	
	public void editOrg(SysOrgDto ot) throws AppException{
		//允许父ID为空
		SysOrgEbo po = null;
		if(ot.getPID() != null && ot.getPID()>0)
			po = getOrg(ot.getPID());
		SysOrgEbo o = new SysOrgEbo();
		BeanUtils.copyProperties(ot, o);
		o.setPORG(po);
    	update(o);
    }
	
	//获得传输协议	
	public SysOrgEbo getOrg(Integer uid) throws AppException{
		return (SysOrgEbo)get(uid);
	}

	public void deleteOrg(SysOrgEbo o) throws AppException{
		removeById(o.getClass(),o.getOID());
    }
	public void deleteOrg(Integer id) throws AppException{
		removeById(SysOrgEbo.class,id);
	}

	//获得传输协议列表
	public List getAllOrgList() throws AppException{
		String hql = " from SysOrgEbo t " ;
		String where = "";
		List<Object> paramList = new ArrayList<Object>();
		if (isNotBlank(where)) {
			where = " where " + where;
		}
		hql = hql + where + " order by t.OID asc ";
		return find(hql,paramList.toArray());		
	}

	//获得组织树
	public List getOrgTree() throws AppException{
		String where = "";
		String hql = " select new com.topfinance.cnaps2.dto.SysOrgDto ";
		hql += "(e.OID,e.name,e.code,e.desc,e.PORG.OID)";
		hql += " from SysOrgEbo e " ;
		List<Object> paramList = new ArrayList<Object>();
		if (isNotBlank(where)) {
			where = " where " + where;
		}
		hql = hql + where + " order by e.OID asc ";
		return find(hql,paramList.toArray());		
	}
	
}
